package cn.xinfei.xdecision.engine.api;

import cn.xinfei.xdecision.common.utils.util.JsonUtils;
import cn.xinfei.xdecision.engine.runner.context.PipelineContext;
import cn.xinfei.xdecision.engine.runner.context.PipelineContextHolder;
import cn.xinfei.xdecision.engine.runner.context.XDecisionContext;
import cn.xinfei.xdecision.engine.runner.service.DecisionResultDetailService;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.concurrent.ConcurrentMap;

@Slf4j
@RestController
@RequestMapping("engine/test")
public class XDecisionTestApi {
    @Autowired
    private DecisionResultDetailService decisionResultDetailService;


    @RequestMapping(value = "/mongo/get", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "getMongoContext")
    public void getMongoContext(String bizId, String decisionId, String engineCode) {

        PipelineContext document = decisionResultDetailService.getDecisionResultDetailByDecisionId(bizId, decisionId, engineCode);

        XDecisionContext context = XDecisionContext.getContext();
        PipelineContext prevPipelineContext = context.getPipelineContext();

        context.setPipelineContext(document);
        PipelineContext nextPipelineContext = context.getPipelineContext();

//        DecisionReentryApiRequest apiRequest=new DecisionReentryApiRequest();
//        apiRequest.setRequestId("20230403192740661DEFAULT00058");
        System.out.println(document);

        ConcurrentMap<String, Object> contextValue = PipelineContextHolder.getContextValue();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("decision_set_id", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        jsonObject.put("request_data",contextValue);
        jsonObject.put("bus_type", "PERSONAL_ACTIVATE");

        System.out.println(JsonUtils.toJsonString(jsonObject));
        System.out.println(jsonObject.toJSONString());

        // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        // 创建Post请求
        HttpPost httpPost = new HttpPost("http://qa1-rcs-provider.testxinfei.cn/modelTask/startModelRun");
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)
            // 我这里利用阿里的fastjson，将Object转换为json字符串;
            // (需要导入com.alibaba.fastjson.JSON包)
//            String jsonString = JSON.toJSONString(jsonObject);
//            String jsonString = JSONObject.toJSONString(jsonObject);
//            String jsonString = JsonUtils.toJsonString(jsonObject);

//            StringEntity entity = new StringEntity(jsonObject.toJSONString(), "UTF-8");
            StringEntity entity = new StringEntity(JsonUtils.toJsonString(jsonObject), "UTF-8");
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
            httpPost.setEntity(entity);
            httpPost.setHeader("Content-Type", "application/json;charset=utf8");

            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();

            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
